﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Fis.Win.Forms
{
    public partial class ReportFilterForm : BaseForm
    {
        public ReportFilterForm()
        {
            InitializeComponent();
            InitializeBinding();
        }


        public override void InitializeBinding()
        {
            var list = from i in new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 }
                       where i <= int.Parse(Fis.Win.Utils.AppSession.Range)
                       select i;

            edRangeFrom.Properties.Items.AddRange(list.ToArray());
            edRangeTo.Properties.Items.AddRange(list.ToArray());

            var range = int.Parse(Fis.Win.Utils.AppSession.Range);
            edRangeFrom.EditValue = (range > 1) ? range - 1 : 1;
            edRangeTo.EditValue = range <= 12 ? range : 12;
            var cache = Fis.Win.Utils.CacheContext.GetCache("TACCOUNT");
            cache.DefaultView.Sort = "FTYPE,FCODE";
            Fis.Win.Utils.UIUtils.LookUpBinding(edAccount, cache, "FDETAILNAME", "SID");

        }

        private void simpleButton2_Click(object sender, EventArgs e)
        {
            DialogResult = System.Windows.Forms.DialogResult.Cancel;
            Close();
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            DialogResult = System.Windows.Forms.DialogResult.OK;
            Close();
        }

        /// <summary>
        /// 期间过滤条件
        /// </summary>
        public string RangeFilter
        {
            get
            {
                int from = int.Parse(edRangeFrom.Text);
                int to = int.Parse(edRangeTo.Text);

                List<int> ranges = new List<int>();
                for (var i = from; i <= to; i++)
                    ranges.Add(i);

                return string.Join(",", ranges.ToArray());
            }
        }

        /// <summary>
        /// 科目过滤条件
        /// </summary>
        public object AccountFilter
        {
            get { return edAccount.EditValue ?? null; }
        }

        private void edRangeFrom_SelectedValueChanged(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(edRangeFrom.Text) || string.IsNullOrEmpty(edRangeTo.Text))
                return;
            int from = int.Parse(edRangeFrom.Text);
            int to = int.Parse(edRangeTo.Text);
            if (from > to)
                edRangeTo.Text = edRangeFrom.Text;
        }
    }
}
